package br.com.fogas.medicao.business;

import java.sql.SQLException;
import java.util.List;

import br.com.fogas.database.entidade.PlanoVisitas;
import br.com.fogas.medicao.activity.PlanoLocalFragment;
import br.com.fogas.medicao.activity.helper.PlanoHomeHelper;
import br.com.fogas.medicao.application.Constants;
import br.com.fogas.medicao.application.LoadFragment;
import br.com.fogas.utils.task.ServiceTask;
import br.com.fogas.utils.task.ServiceTaskBusiness;

public class PlanoHomeBusiness implements ServiceTaskBusiness {
	
	private PlanoHomeHelper helper;

	public PlanoHomeBusiness(PlanoHomeHelper helper) {
		this.helper = helper;
	}

	@Override
	public void start(ServiceTask serviceTask) {
		if(existePlanoCriado()) {
			direcionarPlanoShoppings();
		} else {
			helper.mostrarViews();
		}
	}
	
	private void direcionarPlanoShoppings() {
		LoadFragment loadFragment = new LoadFragment(helper.getFragment().getActivity().getSupportFragmentManager());
		loadFragment.initializeFragment(true, new PlanoLocalFragment(), Constants.FRAGMENT_PLANO_LOCAL);
	}

	private boolean existePlanoCriado() {
		try {
			List<PlanoVisitas> visitas = helper.getDatabase().getPlanoVisitas().queryForAll();
			if(!visitas.isEmpty()) {
				return true;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}

}
